package com.mcp.converter.domain.rule.compatibility.function;

import com.mcp.converter.domain.checker.IncompatibilityDetail;
import com.mcp.converter.shared.response.sql.SQLElement;
import com.mcp.converter.domain.rule.compatibility.AbstractFunctionRule;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 数值函数兼容性规则
 * OceanBase中数值函数的处理与Oracle存在差异
 * 
 * @author zengxiong
 */
public class NumberFunctionRule extends AbstractFunctionRule {

    private static final String RULE_ID = "FN009";
    private static final String RULE_NAME = "数值函数不兼容";
    private static final String RULE_DESCRIPTION = "OceanBase中不支持以下高精度数值计算函数";

    private static final Set<String> NUMBER_FUNCTIONS = new HashSet<>(Arrays.asList(
        "APPROX_COUNT",
        "APPROX_COUNT_DISTINCT_AGG",
        "APPROX_COUNT_DISTINCT_DETAIL",
        "APPROX_MEDIAN",
        "APPROX_PERCENTILE",
        "APPROX_PERCENTILE_AGG",
        "APPROX_PERCENTILE_DETAIL",
        "LOG10",
        "FUSION"
    ));

    public NumberFunctionRule() {
        super(RULE_ID, RULE_NAME, RULE_DESCRIPTION);
        setIncompatibilityDetail(
            "OceanBase不支持以下数值计算函数：\n" +
            "1. APPROX_COUNT: \n" +
            "   - 用于近似计数的聚合函数\n" +
            "   - Oracle专有实现\n" +
            "2. APPROX_COUNT_DISTINCT_AGG: \n" +
            "   - 用于近似不同值计数的聚合函数\n" +
            "   - 提供统计信息的近似值\n" +
            "3. APPROX_COUNT_DISTINCT_DETAIL: \n" +
            "   - 提供详细的近似计数信息\n" +
            "   - 支持更复杂的统计分析\n" +
            "4. APPROX_MEDIAN: \n" +
            "   - 计算近似中位数的函数\n" +
            "   - 适用于大数据集的快速估算\n" +
            "5. APPROX_PERCENTILE: \n" +
            "   - 计算近似百分位数\n" +
            "   - 提供快速的大数据集统计\n" +
            "6. APPROX_PERCENTILE_AGG: \n" +
            "   - 聚合型百分位数计算\n" +
            "   - 用于复杂统计分析\n" +
            "7. APPROX_PERCENTILE_DETAIL: \n" +
            "   - 提供详细的百分位数信息\n" +
            "   - 支持多维数据分析\n" +
            "8. APPROX_RANK: \n" +
            "   - 近似排名计算\n" +
            "   - 适用于大数据集的快速排名\n" +
            "9. APPROX_SUM: \n" +
            "   - 近似求和函数\n" +
            "   - 用于大数据集的快速估算\n" +
            "10. LOG10: \n" +
            "    - 计算以10为底的对数\n" +
            "    - 在数学计算中有特定用途\n" +
            "11. FUSION: \n" +
            "    - OceanBase不支持该函数\n" +
            "    - 不支持嵌套集合类型（TABLE OF ...）\n" +
            "    - 不支持用于多值合并的聚合操作\n" +
            "    - 无多值自动去重行为\n" +
            "    - 需要使用普通聚合+去重逻辑替代",
            "可能导致：\n" +
            "1. 统计结果不准确\n" +
            "2. 性能问题\n" +
            "3. 数据分析错误\n" +
            "4. 查询执行失败\n" +
            "5. 多值合并操作失败\n" +
            "6. 去重逻辑需手动实现",
            "建议：\n" +
            "1. 使用OceanBase支持的替代函数\n" +
            "2. 将复杂统计计算移至应用层\n" +
            "3. 对关键统计指标进行验证\n" +
            "4. 建立统计结果的校验机制\n" +
            "5. 对比不同数据库平台的计算结果\n" +
            "6. 使用普通聚合函数配合DISTINCT实现去重\n" +
            "7. 手动实现多值合并逻辑",
            IncompatibilityDetail.Severity.HIGH
        );
    }

    @Override
    public boolean matches(Object target) {
        if (target instanceof SQLElement) {
            SQLElement element = (SQLElement) target;
            if (isFunctionElement(element)) {
                String functionName = element.getName().toUpperCase();
                return NUMBER_FUNCTIONS.contains(functionName);
            }
        }
        return false;
    }
    
    @Override
    public Set<String> getIncompatibleFunctions() {
        return new HashSet<>(NUMBER_FUNCTIONS);
    }
}